package com.example.mybatisplus.service.impl;

import com.example.mybatisplus.model.domain.*;
import com.example.mybatisplus.mapper.UserMapper;
import com.example.mybatisplus.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import static com.example.mybatisplus.common.Constants.PROCEDURE_UNDERWAY;
import static com.example.mybatisplus.common.Constants.ROLE_FIRSTVISIT;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lxp
 * @since 2022-05-31
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SingleTimeService singleTimeService;

    @Autowired
    private TeacherRotaService teacherRotaService;

    @Autowired
    private ConsultMergeService consultMergeService;

    @Autowired
    private VisitLogService visitLogService;

    public User getUserbyName(String name){
        return userMapper.getUserbyName(name);
    }
    public User getConsultantById(Long id){
        return userMapper.getConsultantById(id);
    }

    public List<User> getConsultants(){
        return userMapper.getConsultants();
    }

    public Long getIdByName(String username){
        return userMapper.getIdByName(username);
    }
    public Long getTeacherIdByName(String teacherName){
        return userMapper.getTeacherIdByName(teacherName);
    }

    public List<User> getSpareConsultantsByTimeId(Long id) {
        SingleTime singleTime = singleTimeService.getById(id);
        System.out.println(singleTime);
        int start = 0;
        int end = 0;
        if (singleTime.getTimeDay().length() == 9) {
            start = Integer.parseInt(singleTime.getTimeDay().substring(0, 1));
            end = Integer.parseInt(singleTime.getTimeDay().substring(5, 6));
            System.out.println(start);
            System.out.println(end);
        }
        if (singleTime.getTimeDay().length() == 10) {
            start = Integer.parseInt(singleTime.getTimeDay().substring(0, 1));
            end = Integer.parseInt(singleTime.getTimeDay().substring(5, 7));
            System.out.println(start);
            System.out.println(end);
//            start = Integer.parseInt(singleTime.getTimeDay().substring(0,1));
        }
        if (singleTime.getTimeDay().length() == 11) {
            start = Integer.parseInt(singleTime.getTimeDay().substring(0, 2));
            end = Integer.parseInt(singleTime.getTimeDay().substring(6, 8));
            System.out.println(start);
            System.out.println(end);
//            start = Integer.parseInt(singleTime.getTimeDay().substring(0,1));
        }
        if(end == 12){
            end = 0;
        }
        int Weekday = 0;
        if (singleTime.getWeakDay().equals("周日")) {
            Weekday = 1;
        } else if (singleTime.getWeakDay().equals("周一")) {
            Weekday = 2;
        } else if (singleTime.getWeakDay().equals("周二")) {
            Weekday = 3;
        } else if (singleTime.getWeakDay().equals("周三")) {
            Weekday = 4;
        } else if (singleTime.getWeakDay().equals("周四")) {
            Weekday = 5;
        } else if (singleTime.getWeakDay().equals("周五")) {
            Weekday = 6;
        } else if (singleTime.getWeakDay().equals("周六")) {
            Weekday = 7;
        }
        System.out.println("Weekday:" + Weekday + " 1" + singleTime.getWeakDay() + "1");


        System.out.println("#######################");
        Calendar startDate = Calendar.getInstance();
        Calendar endDate = Calendar.getInstance();
        Calendar calendar = Calendar.getInstance();
        startDate.setTime(new Date());
        endDate.setTime(new Date());
        System.out.println("当前是周" + startDate.get(Calendar.DAY_OF_WEEK));
        if (startDate.get(Calendar.DAY_OF_WEEK) > Weekday && Weekday != 1) {
            startDate.add(Calendar.DATE, 7-(startDate.get(Calendar.DAY_OF_WEEK)-Weekday));
            endDate.add(Calendar.DATE, 7-(startDate.get(Calendar.DAY_OF_WEEK)-Weekday));
        } else if (startDate.get(Calendar.DAY_OF_WEEK) == Weekday){
            System.out.println(Calendar.HOUR);
            int tempHour = calendar.get(Calendar.HOUR);
            int tempMinute = calendar.get(Calendar.MINUTE);
            if(start<=tempHour&&tempHour<=end){
                if(end == tempHour){
                    if(tempMinute == 0){

                    } else {
                        startDate.add(Calendar.DATE, 7);
                        endDate.add(Calendar.DATE, 7);
                    }
                } else {
                    startDate.add(Calendar.DATE, 7);
                    endDate.add(Calendar.DATE, 7);
                }
            } else {
                startDate.add(Calendar.DATE, 7);
                endDate.add(Calendar.DATE, 7);
            }
        }
        endDate.set(Calendar.DAY_OF_WEEK, Weekday);
        endDate.set(Calendar.HOUR, end);
        endDate.set(Calendar.MINUTE, 0);
        endDate.set(Calendar.SECOND, 0);
        startDate.set(Calendar.DAY_OF_WEEK, Weekday);
        startDate.set(Calendar.HOUR, start);
        startDate.set(Calendar.MINUTE, 0);
        startDate.set(Calendar.SECOND, 0);
        System.out.println("开始时间:" + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(startDate.getTime()));
        System.out.println("结束时间:" + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(endDate.getTime()));
        System.out.println("#######################");
        List<User> users = userMapper.getConsultants();
        List<TeacherRota> teacherRotas = teacherRotaService.list();
        List<ConsultMerge> consultMerges = consultMergeService.list();
        List<VisitLog> visitLogs = visitLogService.list();
        List<User> spareConsultants = new ArrayList<>();

        /**
         * 查找
         */
        for (User u : users) {
            for (TeacherRota t : teacherRotas) {
                if (u.getId().toString().equals(t.getTeaId().toString()) && t.getTimeId().toString().equals(singleTime.getId().toString())) {
                    boolean flag = true;
                    /**
                     * 如果咨询表中该时段已经有安排
                     */
                    for (ConsultMerge c : consultMerges) {
                        if (c.getTeaId().toString().equals(t.getTeaId().toString())) {
                            System.out.println("***************************");
                            System.out.println("咨询表中的开始日期："+new SimpleDateFormat("yyyy-MM-dd hh:mm").format(c.getStartDate()));
                            System.out.println("开始日期："+new  SimpleDateFormat("yyyy-MM-dd hh:mm").format(startDate.getTime()));
                            System.out.println("咨询表中的结束日期："+new SimpleDateFormat("yyyy-MM-dd hh:mm").format(c.getEndDate()));
                            System.out.println("结束日期："+new  SimpleDateFormat("yyyy-MM-dd hh:mm").format(endDate.getTime()));
                            System.out.println("***************************");
//                            if (new SimpleDateFormat("yyyy-MM-dd").format(c.getStartDate()).equals(new  SimpleDateFormat("yyyy-MM-dd").format(startDate.getTime()))) {
//                                flag = false;
//                            }
                            if ((startDate.getTime().after(c.getStartDate()) && startDate.getTime().before(c.getEndDate())) || (endDate.getTime().after(c.getStartDate()) && endDate.getTime().before(c.getEndDate()))){
                                System.out.println("不符合！！！！！");
                                System.out.println("***************************");
                                System.out.println();
                                flag = false;
                            }
                        } else {
                            continue;
                        }
                    }
                    if (!flag) {
                        if (spareConsultants.contains(u)){
                            spareConsultants.remove(u);
                        }
                        break;
                    } else {
                        if (!spareConsultants.contains(u)){
                            spareConsultants.add(u);
                        }
                    }
                }
            }
        }
        System.out.println(spareConsultants);
        return spareConsultants;
    }

    @Override
    public List<User> getRole(int role) {
        return userMapper.getRole(role);
    }

    @Override
    public List<User> getSpareFirstvisitsByTimeId(Long id) {
        Date start = consultMergeService.getStarttime(id);
        List<User> users = userMapper.getRole(ROLE_FIRSTVISIT);
        List<TeacherRota> rotas = teacherRotaService.list();
        List<VisitLog> visitLogs = visitLogService.list();
        List<User> result = new ArrayList<>();
        for(TeacherRota r : rotas){
            for(User u : users){
                if(r.getTeaId().toString().equals(u.getId().toString())){
                    result.add(u);
                }
            }
        }
        for(VisitLog v : visitLogs){
            if(v.getVisitStatus() == PROCEDURE_UNDERWAY && new SimpleDateFormat("yyyy-MM-dd").format(v.getStartTime()).equals(new SimpleDateFormat("yyyy-MM-dd").format(start))){
                System.out.println();
                System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$");
                System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(v.getStartTime()));
                System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(start));
                System.out.println();
                System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$");
                System.out.println();
                for(TeacherRota r : rotas){
                    if(r.getTimeId().toString().equals(v.getTimeId().toString())){
                        for (User u : users){
                            if(u.getId().toString().equals(v.getTeaId().toString())){
                                result.remove(u);
                            }
                        }
                    }
                }
            }
        }
        return result;
    }

    @Override
    public User getByIdAndType(Long id, int role) {
        return userMapper.getByIdAndType(id,role);
    }

    @Override
    public Long getByNameAndType(String teacherName, int role) {
        return userMapper.getByNameAndType(teacherName,role);
    }
}
